package bforce;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.LinkedList;
import java.util.List;

import common.CommonHelper;
import common.GeneticHelper;

import salb1.Salb1GeneticOperations;
import salb1.Salb1Genotype;
import salb1.TimeOfOperations;
import nsga.Individual;


/**
 * Umożliwia podzielenie na kilka części.
 * @author krzysiek
 *
 */
public class BFRunnerParts {

	public static void main(String argv[]) {
	    
		TimeOfOperations top = null;
		BufferedReader individualsReader = null;
		int begIt = 0, itNum = 0, endIt = 0;
		
		if (argv.length != 4) {
		    System.out.println("Skladnia: program [plik_wejsciowy_zadanie] [plik_wyjsciowy_i_wyjsciowy_genotypy]" +
		    		" [od_ktorej_iteracji] [ilosc_iteracji]");
		    System.exit(1);
		}
		try {
			top = new TimeOfOperations(argv[0]);
			if ((new File(argv[1]).canRead())) {
			    individualsReader = new BufferedReader(new FileReader(argv[1])); 
			}
			begIt = Integer.parseInt(argv[2]);
			itNum = Integer.parseInt(argv[3]);
			endIt = begIt + itNum;
			
		}
		catch (Exception e) { 
		    e.printStackTrace();
		    System.exit(0);
		}
		
	    Salb1GeneticOperations go = new Salb1GeneticOperations(top);
	    long all = (long)Math.pow(top.getNumOfStations(),top.getNumOfOperations());
	    
	    System.out.println("Algorytm rozwiazujacy metoda pelnego przegladu od iteracji "+begIt+" do iteracji "+(begIt+itNum));
	    System.out.println("Dane wejsciowy:     "+argv[0]);
	    System.out.println("Najlepsze osobniki: "+argv[1]);
	    System.out.println("ilosc operacji        "+top.getNumOfOperations());
	    System.out.println("ilosc stacji          "+top.getNumOfStations());
	    System.out.println("ilosc wariantow       "+top.getNumOfVariants());
	    System.out.println("wszystkich kombinacji "+all);
		
		List<Individual<Salb1Genotype>> individuals = null;
		if (individualsReader != null) {
		    
		    String str;
		    individuals = new LinkedList<Individual<Salb1Genotype>>();
		    
		    try {
		        while ((str = individualsReader.readLine()) != null) {
		            String genotypeStr = str.split(" ")[2];
		            Salb1Genotype genotype = new Salb1Genotype(genotypeStr);
		            individuals.add(new Individual<Salb1Genotype>(genotype,go));
		            //System.out.println("genotype: nowy "+(new Salb1Genotype(genotype))+" stary "+genotype);
		        }
		        individualsReader.close();
		    }
		    catch (IOException ioe) {
		        ioe.printStackTrace();
		    } 
		}

		long beginMili = System.currentTimeMillis();
		
		//
		// OBLICZENIA
		//
		List<Individual<Salb1Genotype>> resultPopulation = go.createAllResults(individuals, begIt,itNum);
		
		long endMili = System.currentTimeMillis();
		long time = endMili - beginMili;
		long timeToEnd = ((all - endIt) / itNum) * time;
		
		System.out.println("Obliczenia trwaly "+(time/1000)+" sek., do konca zostalo "+(timeToEnd/1000.0/60.0)+" min.");
		
		String resultGenotypePopulationStr = GeneticHelper.populationToString(resultPopulation);
		CommonHelper.printToFile(argv[1], resultGenotypePopulationStr);
		
		String resultValuesPopulationStr = GeneticHelper.populationValuesToString(resultPopulation);
		CommonHelper.printToFile(argv[1]+"_values", resultValuesPopulationStr);
	}

}
